package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.dto.Dto;
import com.bean.pojo.ItripUser;
import com.bean.vo.userinfo.ItripUserVO;
import com.exception.SmsSendException;
import com.service.IItripUserService;
import com.service.MailService;
import com.service.SmsService;
import com.util.DtoUtil;
import com.util.MD5;
import com.util.RedisUtil;
import com.util.SixCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

@RestController
@ResponseBody
public class UserController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private MailService mailService;
    @Autowired
    private IItripUserService iItripUserService;
    @Autowired
    private RedisUtil redisUtil;


    private String code_prefix = "code:";

    private String mail_prefix = "mail:";

    @PostMapping("/api/registerbyphone")
    public @ResponseBody
    Dto registerbyphone(@RequestBody ItripUserVO userVO) {
        System.out.println(">>> 发送验证短信");
        System.out.println("手机:" + userVO.getUserCode());

        System.out.println(userVO);
        //生成6位验证码
        StringBuilder code = SixCodeUtil.SixCode();

        //保存到缓存
        redisUtil.setString(code_prefix + userVO.getUserCode(), code.toString());
        //保存到数据库
        ItripUser user = new ItripUser();
        user.setUserCode(userVO.getUserCode());
        user.setUserPassword(MD5.getMd5(userVO.getUserPassword(), 32));
        user.setUserType(0);
        user.setCreationDate(new Date());
        if (userVO.getUserName() != null) {
            user.setUserName(userVO.getUserName());
        }
        //查找是否在数据库中已注册
        QueryWrapper<ItripUser> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userVO.getUserCode());
        ItripUser one = iItripUserService.getOne(wrapper);
        if (one != null) {
            return DtoUtil.returnError("false", "此账户已注册", "");
        }

        boolean save = iItripUserService.save(user);

        try {
            smsService.sendMessage(userVO.getUserCode(), code.toString(), "10");
            return DtoUtil.returnSuccess("发送成功！");
        } catch (SmsSendException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return DtoUtil.returnError("发送失败");
        }

    }

    @RequestMapping("/api/validatephone")
    public Object checkCode(String user, String code) {
        System.out.println(">>> 查验验证码");
        System.out.println(user + " : " + code);

        //到缓存中查询
        String val = redisUtil.getString(code_prefix + user);
        //判断
        if (val != null && val.equals(code)) {
            //数据库修改为已激活
            QueryWrapper<ItripUser> wrapper = new QueryWrapper<>();
            wrapper.eq("userCode", user);
            ItripUser user1 = new ItripUser();
            user1.setActivated(1);
            boolean update = iItripUserService.update(user1, wrapper);
            System.out.println("修改" + update);
            //清除验证码缓存
            redisUtil.setString(code_prefix + user, "", 5);
            return DtoUtil.returnSuccess();
        }

        return DtoUtil.returnError("验证码错误!");
    }

    @GetMapping("/api/ckusr")
    public Object ckusr(String name) {
        System.out.println(">>> 查验用户名重复");
        System.out.println(name);

        //数据库修改为已激活
        QueryWrapper<ItripUser> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", name);
        ItripUser user1 = new ItripUser();
        user1.setActivated(1);
        ItripUser one = iItripUserService.getOne(wrapper);
        System.out.println("查用户名" + one);
        //清除验证码缓存
        if (one != null) {
            return DtoUtil.returnError("false", "此账户已注册", "");
        }
        return DtoUtil.returnSuccess("用户可以使用!");
    }

    @PostMapping("/api/doregister")
    public @ResponseBody Object doregister(@RequestBody ItripUserVO userVO){
        System.out.println(">>> 邮箱注册");
        System.out.println("注册邮箱:" + userVO);

        //获取6位随机数
        StringBuilder sb = SixCodeUtil.SixCode();

        //保存激活码
        redisUtil.setString(mail_prefix + userVO.getUserCode(), sb.toString());

        //在sql中添加数据
        ItripUser user = new ItripUser();
        user.setUserCode(userVO.getUserCode());
        user.setUserPassword(MD5.getMd5(userVO.getUserPassword(), 32));
        user.setUserType(0);
        user.setCreationDate(new Date());
        if (userVO.getUserName() != null) {
            user.setUserName(userVO.getUserName());
        }
        //查找是否在数据库中已注册
        QueryWrapper<ItripUser> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userVO.getUserCode());
        ItripUser one = iItripUserService.getOne(wrapper);
        if (one != null) {
            return DtoUtil.returnError("false", "此账户已注册", "");
        }

        boolean save = iItripUserService.save(user);

        //发送邮件
        try {
            mailService.sendActiveMail(userVO.getUserCode(), sb.toString());
            return DtoUtil.returnSuccess("注册成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("注册失败!");
        }
    }

    @RequestMapping("/api/activate")
    public Object activate(String user, String code) {
        System.out.println(">>> 查验验证码");
        System.out.println(user + " : " + code);

        //到缓存中查询
        String val = redisUtil.getString(mail_prefix + user);
        //判断
        if (val != null && val.equals(code)) {
            //数据库修改为已激活
            QueryWrapper<ItripUser> wrapper = new QueryWrapper<>();
            wrapper.eq("userCode", user);
            ItripUser user1 = new ItripUser();
            user1.setActivated(1);
            boolean update = iItripUserService.update(user1, wrapper);
            System.out.println("修改" + update);
            //清除验证码缓存
            redisUtil.setString(mail_prefix + user, "", 5);
            return DtoUtil.returnSuccess();
        }

        return DtoUtil.returnError("验证码错误!");
    }

}
